<template>
  <div class="app-container">
    <el-container>
      <el-header style="height: auto; padding: unset">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" @submit.native.prevent v-show="showSearch" label-width="68px">
          <el-form-item label="条码" prop="barCode">
            <el-input
              v-model="queryParams.barCode"
              placeholder="请输入条码"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="物料编号" prop="productNo">
            <el-input
              v-model="queryParams.productNo"
              placeholder="请输入物料编号"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="批次号" prop="batchNo">
            <el-input
              v-model="queryParams.batchNo"
              placeholder="请输入批次号"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="生产日期" prop="productionDateStr">
            <el-date-picker
              v-model="queryParams.productionDateStr"
              placeholder="请选择生产日期"
              style="width: 100%"
              value-format="yyyy-MM-dd"
              type="date"
            ></el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      </el-header>


      <el-main style="padding: unset">
        <el-table v-loading="loading" :data="businessList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center" />
          <af-table-column label="条码" align="center" prop="barCode" />
          <af-table-column label="物料编号" align="center" prop="productNo" />
          <af-table-column label="物料名称" align="center" prop="productName" />
          <af-table-column label="数量" align="center" prop="quantity" />
          <af-table-column label="状态" align="center" prop="status" >
            <template slot-scope="scope">
              <dict-tag :options="dict.type.sh_batch_status" :value="scope.row.status"/>
            </template>
          </af-table-column>
          <af-table-column label="批次号" align="center" prop="batchNo" />
          <af-table-column label="生产日期" align="center" prop="productionDate" />
          <af-table-column label="有效期" align="center" prop="effectiveDate" />
          <af-table-column label="供应商" align="center" prop="supplierName" />
          <af-table-column label="订单号" align="center" prop="orderNo" />
    <!--       <el-table-column prop="status" label="状态" width="80">-->
    <!--         <template slot-scope="scope">-->
    <!--           <dict-tag :options="dict.type.sh_batch_status" :value="scope.row.status"/>-->
    <!--         </template>-->
    <!--       </el-table-column>-->
          <el-table-column label="创建人" align="center" prop="createBy" min-width="152"/>
          <el-table-column label="创建时间" align="center" prop="createTime"  width="152">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column label="最后更新人" align="center" prop="updateBy" min-width="152"/>
          <el-table-column label="最后更新时间" align="center" prop="createTime"  width="152">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.updateTime) }}</span>
            </template>
          </el-table-column>

        </el-table>
      </el-main>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-container>

    <!-- 添加或修改批次记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="编号" prop="no">
          <el-input v-model="form.no" placeholder="请输入编号" />
        </el-form-item>
        <el-form-item label="条码" prop="barCode">
          <el-input v-model="form.barCode" placeholder="请输入条码" />
        </el-form-item>
        <el-form-item label="物料编号" prop="productNo">
          <el-input v-model="form.productNo" placeholder="请输入物料编号" />
        </el-form-item>
        <el-form-item label="数量" prop="quantity">
          <el-input v-model="form.quantity" placeholder="请输入数量" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listBusiness, getBusiness, delBusiness, addBusiness, updateBusiness } from "@/api/storehouse/barcode/barcode";

export default {
  dicts: ['sh_batch_status'],
  name: "Business",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 批次记录表格数据
      businessList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        no: null,
        barCode: null,
        productNo: null,
        batchNo: null,
        quantity: null,
        productionDateStr: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        no: [
          { required: true, message: "编号不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询批次记录列表 */
    getList() {
      this.loading = true;
      listBusiness(this.queryParams).then(response => {
        this.businessList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        no: null,
        barCode: null,
        productNo: null,
        quantity: null,
        status: 0,
        createBy: null,
        updateBy: null,
        updateTime: null,
        createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加批次记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getBusiness(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改批次记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateBusiness(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBusiness(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm(this.$t('deleteTip', { num: ids.length })).then(function() {
        return delBusiness(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/business/export', {
        ...this.queryParams
      }, `business_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
